package com.pkk.alert.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkk.alert.common.constant.AlertConstant;
import com.pkk.alert.common.constant.RedisKeyConstant;
import com.pkk.alert.common.util.PenguinAlertUtil;
import com.pkk.alert.entity.feige.FeigeAccount;
import com.pkk.alert.mybatis.FeigeAccountMapper;
import com.pkk.alert.service.FeigeAccountService;
import com.pkk.support.cache.client.CacheClient;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author peikunkun<br>
 * @versio V1.0<br>
 * @Title: a<br>
 * @Description: 飞鸽的账户信息表<a><br>
 * @date 2017年2019/7/6 0006 上 11:05<br>
 * @URL:[a]
 */
@Slf4j
@Service
public class FeigeAccountServiceImpl extends ServiceImpl<FeigeAccountMapper, FeigeAccount> implements
    FeigeAccountService {


  @Resource
  private CacheClient cacheClient;
  //@Resource
  //private EmailNotifySupport emailNotifySupport;
  //@Resource
  //private EmailNotifyProperties emailNotifyProperties;

  /**
   * @Description: 获取可以使用的账户信息
   * @Param: []
   * @return: com.pkk.alert.entity.feige.FeigeAccount
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 下午 5:00
   */
  @Override
  public FeigeAccount getUseFeigeAccount() {
    QueryWrapper<FeigeAccount> queryWrapper = new QueryWrapper<FeigeAccount>();
    queryWrapper.eq("statis", "1");
    final List<FeigeAccount> feigeAccounts = baseMapper.selectList(queryWrapper);

    boolean result = false;
    for (FeigeAccount feigeAccount : feigeAccounts) {
      //使用的数量
      String useCount = cacheClient.get(RedisKeyConstant.getKeyFeigeUseAccount(feigeAccount.getId()));
      if (StringUtils.isBlank(useCount)) {
        useCount = "0";
      }
      if (Integer.parseInt(useCount) < AlertConstant.FEIGE_USE_COUNT_FINAL) {
        result = true;
        return feigeAccount;
      }
    }
    if (!result) {
      String content = "没有可以使用的飞鸽账户了-请补充";
      log.error(content);
      //emailNotifySupport.send(emailNotifyProperties.getUsername(), new String[]{AlertConstant.receiveEmailUser}, FeigeAlarmEnum.TYPE_ALARM.getDesc(), content);
      throw new RuntimeException(content);
    }
    return null;
  }


  /**
   * @Description: 操作本月的redis使用量
   * @Param: [accountId]
   * @Param: [ completeNum]:完成数量
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2019/7/7 15:08
   */
  @Override
  public boolean successFeigeAccount(Long accountId, Integer completeNum) {
    //当前时间距离月底的秒数
    final Long nowToLastSecordDayOfMonth = PenguinAlertUtil.getNowToLastSecordDayOfMonth();
    cacheClient
        .incrByEx(RedisKeyConstant.getKeyFeigeUseAccount(accountId), completeNum, nowToLastSecordDayOfMonth.intValue());
    return true;
  }

  /**
   * 处理账户使用信息
   *
   * @return com.alibaba.fastjson.JSONObject
   * @Param
   * @author peikunkun
   * @date 2020/12/1 0001 上午 11:16
   * @since
   */
  @Override
  public JSONObject handleAccountUserInfo() {
    QueryWrapper<FeigeAccount> queryWrapper = new QueryWrapper<FeigeAccount>();
    queryWrapper.eq("statis", "1");
    final List<FeigeAccount> feigeAccounts = baseMapper.selectList(queryWrapper);

    final JSONObject accountInfo = new JSONObject();
    for (FeigeAccount feigeAccount : feigeAccounts) {
      //使用的数量
      String useCount = cacheClient.get(RedisKeyConstant.getKeyFeigeUseAccount(feigeAccount.getId()));
      if (StringUtils.isBlank(useCount)) {
        useCount = "0";
      }

      accountInfo.put(feigeAccount.getAccountName(), useCount);
    }
    return accountInfo;
  }
}
